package interview.juc;

import lombok.val;

import java.util.concurrent.CountDownLatch;

/**
 * @Author: zhangrongqing
 * @Date: 2019/12/5 17:15
 */
public class CountDownLatchTestCase {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        Long startTime = System.currentTimeMillis();

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                int sum = 0;

                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        sum += j;
                    }
                    try {
                        Thread.sleep(1000);     //为了更加明显
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread name:" + Thread.currentThread().getName() + "sum:" + sum);
                    countDownLatch.countDown();
                }
            }).start();
        }
        countDownLatch.await(); //等待所有线程完成
        Long endTime = System.currentTimeMillis();
        System.out.println("System run Time:" + (endTime - startTime));
    }
}
